var server = require('./cc.js');
var udp = require('./udpserver.js');
var server_post = require('./server_post.js');
var define = require("./db_define.js");
//var mysql = require('mysql');




var obj = {};

obj.port = 8052; //tcp端口启动为8051
obj.protocol = {
    //start: { len: 1, c: 0x69 },
    contentlen: { offset: 1, len: 2, includeht: 1 }, //includehead 为0 不包含头部 1 包含头部和其他
    cmd: { offset: 3, len: 2 },
    deviceid: { offset: 5, len: 4 },
    headfields: {
        len: 8, field: [
            //time6 6字节
            { name: "time", offset: 9,type:"time6", len: 6 },
            { name: "version", offset: 15,type:"int8", len: 1 },
            { name: "aftercmd", offset: 16,type:"int8", len: 1 }
        ]
    },
    crc: { len: 2 },
    bodyfields: [
        {
            //type: string float double uint32 int32 uint16 int16 int8 uint8
            //ftype bige 大端格式数据 lite小端数据格式
            cmd: 0x0101,
            field: [
           { name: "temp", type: "float", ftype: "bige", len: 4 },
           { name: "high", type: "int16", ftype: "bige", len: 2 },
           { name: "a", ftype: "float", ftype: "bige", len: 4 }]
        },
        {
            cmd: 0x0201,//心跳包
            des:"心跳包",
            field:null
        },
        {
            cmd: 0x0202,
            field: [
                { name: "cycle", des: "采集周期", type: "int8", ftype: "bige", len: 1 },
                { name: "sig", des: "信号强度", type: "int8", ftype: "", len: 1 },
                { name: "dl", des: "电池电量", type: "int8", ftype: "", len: 1 },
                { name: "num1", des: "传感器种类数目", type: "int8", ftype: "", len: 1 },
                { name: "num2", des: "传感器种类数目", type: "int8", ftype: "", len: 1 },
                { name: "num3", des: "传感器种类数目", type: "int8", ftype: "", len: 1 },
                { name: "num4", des: "传感器种类数目", type: "int8", ftype: "", len: 1 },
                {
                    name: "protocol",
                    des: "传感器数据体子协议命令0x0202包含子协议数据",
                    type: "protocol",
                    ftype:"leftcycle", //剩余字节循环
                    len: 0,
                    field_child: [
                        //数量
                        { name: "sensor_num", des: "传感器数量", type: "int8", ftype: "", len: 1 },
                        //字节 数量*字节 就等于数据体,注意是不断循环的,ftype "2:1" 表明在每一个段落的第2字节,偏移量为1,从第1位开始
                        { name: "data_len", des: "传感器字节数类-型字节数-高四位", type: "bit",ftype:"1", len: 4 },
                        { name: "dm", des: "正负", type: "bit", ftype: "5", len: 1 },
                        { name: "point_num", des: "小数位数", type: "bit", ftype: "6", len: 3 },
                    ]
                }
            ]
        },
        {
            cmd: 0x0104,
            field: [
                { name: "test", type: "integer", ftype: "bige", len: 4 }
            ]
        }
    ],
    crc: { len: 2 },
    //end: { len: 1, c: 0x16 }
};



var post_option = [
    {
    option :null,
    url:"http://118.190.203.202:9069/sensor_data_1/8052"
    },
    {
        option: null,
        url:"http://120.27.18.161:3001/api/system/device_info"
    }
]

function cb(ret, info) {
    if (ret == 500)
        console.log("post error info:", info);
}

function callback(data) {
   
    //如果远程地址有,则post上去
    var deviceid = data.deviceid;
    var time = data.headfield.time;//time

    var pdata_extern = {};
    Object.assign(pdata_extern, data.field);
    pdata_extern.deviceid = deviceid;
    pdata_extern.time = time;

    for (var i = 0; i < data.protocol.length; i++) {
        var pdata = {};
        Object.assign(pdata, data.protocol[i].head);
        pdata.deviceid = deviceid;

        pdata.time = time;//time
        pdata.value = JSON.stringify(data.protocol[i].value);
        pdata.name = "sensor:" + (i + 1);
        //if (deviceid == '00010001') {
        //    console.log(pdata);
        //}

        //console.log(pdata);
        server_post.post_data(pdata, post_option[1].option, null, cb);
        switch (pdata.sensor_num) {
            case 1:
                pdata.name = "pos";
                break;
            case 2:
                pdata.name = "flow";
                break;
            case 12:
                pdata.name = "temp";
                break;
        }
        //console.log(pdata);
        var data_1 = {};
        if (i == 0)
            data_1.data_ext = pdata_extern;
        data_1.data = pdata;

        //console.log(data_1);
        server_post.post_data(data_1, post_option[0].option, null, cb);
    }
/*
    { data_ext:
            { cycle: 10,
                sig: 30,
                dl: 96,
                num1: 2,
                num2: 0,
                num3: 0,
                num4: 0,
                deviceid: '00010005',
                time: '2017-12-26 7:51:12' },
        data:
            { sensor_num: 12,
                data_len: 2,
                point_num: 4,
                dm: 1,
                deviceid: '00010005',
                time: '2017-12-26 7:51:12',
                value: '[57,71,72,89,341,135,130,108,103,85,80,55]',
                name: 'sensor:1' } }
*/
    //server_post.post_data({data:null,data_extern:pdata_extern}, option, null, cb);
    //向数据库增加数据
}


function callback_udp(data) {
    var obj = JSON.parse(data);
    console.log("send to sensor:", obj);
    send2sensor_ask(obj.deiveid)
}
for (var i = 0; i < post_option.length; i++) {
    if (post_option[i].option == null && post_option[i].url != null)
        post_option[i].option = server_post.parseurl(post_option[i].url);
}
server.ProcessTcpServer(obj.protocol, obj.port, callback);
